package com.cobra.mall.domain.goods.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cobra.mall.domain.goods.entity.GoodsEntity;
import com.cobra.mall.domain.goods.entity.request.GoodsRequest;
import com.cobra.mall.domain.goods.service.inter.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @PostMapping
    public String create(@RequestBody GoodsRequest goodsRequest) {
        GoodsEntity goodsEntity = new GoodsEntity();
        goodsEntity.setGoodsName(goodsRequest.getGoodsName()).setStock(goodsRequest.getStock());
        goodsService.save(goodsEntity);
        return "success";
    }
    @GetMapping("/{goodsId}")
    public GoodsEntity select(@PathVariable("goodsId") Long goodsId) throws Exception {
       return goodsService.getByIdWithCache(goodsId);
    }
    @PutMapping("/updateStock/{goodsId}/{stock}")
    public GoodsEntity updateStock(@PathVariable("goodsId") Long goodsId,@PathVariable("stock") int stock) throws Exception {
         goodsService.update(new UpdateWrapper<GoodsEntity>().eq("id",goodsId).set("stock",stock));
         return goodsService.getByIdWithCache(goodsId);
    }

}
